<?php

class UserBookings_model extends CI_Model
{
	function getTheBookings($id)
	{	
		
		
		$sqlCommand="SELECT * FROM bokning WHERE KundID = ? order by startTid asc";
		$query=$this->db->query($sqlCommand,array($id));
		
		
		if ($query->num_rows() >= 1)
		{	
			
			return $query;
		}
		
		else
		{
			return "Inga bokningar";
		}
	}
	
	function getCarName($id)
	{
		
		$sqlCommand='SELECT * FROM bil WHERE ID = ?';
		
		$query=$this->db->query($sqlCommand,array($id));
		
			if ($query->num_rows() == 1)
		{	
			$theCar = $query->row();
			return $theCar;
		}
		
		else
		{
			return "Hittade inte bilen";
		}
	}
	
	function getBookingFromID($bookingID, $UserID)
	{
	
		$sqlCommand='SELECT * FROM bokning WHERE ID = ?';
		
		$query=$this->db->query($sqlCommand,array($bookingID));
		
		if ($query->num_rows() == 1)
		{	
			$theBooking = $query->row();
			
			if($theBooking->KundID == $UserID)
			{
				return $theBooking;
			}
			
			else
			{
				return FALSE;
			}
			
		}
		
		else
		{
			return FALSE;
		}
	}
	
	function cancelBooking($bookingID, $UserID)
	{
		$sqlCommand='SELECT * FROM bokning WHERE ID = ?';
		
		$query=$this->db->query($sqlCommand,array($bookingID));
		
			if ($query->num_rows() == 1)
		{	
			$theBooking = $query->row();
			
			if($theBooking->KundID == $UserID)
			{
				
				
				$sqlCommand='DELETE FROM bokning WHERE ID= ?';
		
				$query=$this->db->query($sqlCommand,array($bookingID));
				
				
				return $query;
				
			}
			
			else
			{
				return "Fel annvändare inloggad för bokning";
			}
			
		}
		
		else
		{
			return "Hittade inte bokningen";
		}
	}
}
	